﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace CloudTaobao
{
    public partial class WangWangPage : PhoneApplicationPage
    {
        Find fd = new Find();
        public WangWangPage()
        {
            InitializeComponent();

            fd.startLoadingEvent += new StartLoadingEventHandler(fd_startLoadingEvent);
            fd.endLoadingEvent += new EndLoadingEventHandler(fd_endLoadingEvent);
            RecentContactorList.ItemsSource = fd.recentContactorList;
        }

        void fd_endLoadingEvent()
        {
            LoadingBar.IsIndeterminate = false;
            LoadingBar.Visibility = Visibility.Collapsed;
        }

        void fd_startLoadingEvent(object source, StartLoadingEventArgs args)
        {
            LoadingBar.IsIndeterminate = true;
            LoadingBar.Visibility = Visibility.Visible;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            App app = Application.Current as App;
            if (!app.sessionInfo.ContainsKey("top_session"))
            {
                if (MessageBox.Show("抱歉^_^您需要登录才能使用此功能,是否转向登录页面？", "缺少登录信息",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
                }
                else
                {
                    NavigationService.GoBack();
                }
                return;
            }
            myTitle.Text = app.sessionInfo["nick"];
            try
            {
                fd.GetRecentContactor();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void RecentContactorList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RecentContactorList.SelectedItem == null)
            {
                return;
            }
            UriBuilder uri = new UriBuilder();
            uri.Query = "uid=" + ((Contactor)RecentContactorList.SelectedItem).UID + "&itemId=";
            NavigationService.Navigate(new Uri("/MessagePage.xaml" + uri.Query, UriKind.Relative));
        }
    }
}